//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using OpenLS.Core;

namespace OpenLS.Chart.UI
{
    class FormattingConverter: IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            object value = values[0];
            if (value == DependencyProperty.UnsetValue)
                return "<unset value>";
            var format = StrictConverter.Convert<Format>( values[1]);
            var unit = StrictConverter.Convert<Unit>(values[2]);
            if (unit != null && value is double)
            {
                var dbl = (double) value;
                value = UnitConverter.Convert(dbl, unit);
            }
            if (format == null)
                format = Formats.General;
            //if (value is double && format is DateTimeFormat)
           // {
                IFormatValueConverter conv;
                if (values.Length < 3 || !(values[3] is IFormatValueConverter))
                    conv = new DateTimeConverter();
                else
                {
                    conv = (IFormatValueConverter)values[3];
                }
            //    value = conv.DoubleToDateTime((double) value);
            //}
            object result = format.GetFormattedValue(value, conv);
            if (targetType == typeof(string) && result != null)
                result =  result.ToString();
            return result;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
